"""
LLM客户端工厂
"""

from typing import Dict, Type

from ..logger import setup_logger
from .baichuan_client import BaiChuanClient
from .base_client import BaseClient
from .openai_client import LLMClient
from .wenxin_client import WenXinClientV1, WenXinClientV2

logger = setup_logger(__name__)

# 客户端类型映射
CLIENT_TYPES: Dict[str, Type[BaseClient]] = {
    "openai": LLMClient,
    "ernie": WenXinClientV2,
    "baichuan": BaiChuanClient,
    "default": LLMClient
}


def create_client(model_name: str) -> BaseClient:
    """
    根据模型名称创建对应的客户端
    
    Args:
        model_name: 模型名称
        
    Returns:
        BaseClient: 对应的客户端实例
    """
    try:
        # 根据模型名称前缀确定客户端类型
        client_type = "default"
        for prefix, client_class in CLIENT_TYPES.items():
            if model_name.lower().startswith(prefix):
                client_type = prefix
                break

        # 创建并返回客户端实例
        client_class = CLIENT_TYPES[client_type]
        return client_class(model_name=model_name)
    except Exception as e:
        logger.error(f"Failed to create client for {model_name}: {e}")
        raise
